package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    //检查组分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("检查组分页查询");
        try {
            PageResult pageResult = checkGroupService.findPage(queryPageBean);
            return pageResult;
        }
        catch (Exception e) {
            log.error("检查组分页查询失败: {}", e.getMessage(), e);
            throw new RuntimeException("检查组分页查询失败，请稍后重试");
        }
    }

    //根据id删除检查组
    @GetMapping("/deleteById")
    public Result deleteById( @RequestParam("id") Integer id){
        log.info("根据id删除检查组");
        try {
            checkGroupService.deleteById(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    //新增检查组
    @PostMapping("/add")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkitemIds){
        log.info("新增检查组");
        try {
            checkGroupService.addCheckGroup(checkGroup, checkitemIds);
            return new Result(true, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增失败");
        }
    }

    //根据id查询检查组
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询检查组");
        try {
            return new Result(true, "查询成功", checkGroupService.findById(id));
        }
        catch (Exception e) {
            log.error("根据id查询检查组失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据id查询检查组失败，请稍后重试");
        }
    }

    //编辑检查组
    @PostMapping("/edit")
    public Result editEdit(@RequestBody CheckGroup checkGroup,@RequestParam Integer[] checkitemIds){
        log.info("编辑检查组:{},{}", checkGroup, Arrays.toString(checkitemIds));
        try {
            checkGroupService.editCheckGroup(checkGroup, checkitemIds);
            return new Result(true, "编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
    }

    //查询所有检查组
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组");
        try {
            return new Result(true, "查询成功", checkGroupService.findAll());
        }
        catch (Exception e) {
            log.error("查询所有检查组失败: {}", e.getMessage(), e);
            throw new RuntimeException("查询所有检查组失败，请稍后重试");
        }
    }

    //根据套餐id查询检查组id
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId){
        log.info("根据套餐id查询检查组id:{}", setmealId);
        try {
            return new Result(true, "查询成功", checkGroupService.findCheckGroupIdsBySetmealId(setmealId));
        } catch (Exception e) {
            log.error("根据套餐id查询检查组id失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据套餐id查询检查组id失败，请稍后重试");
        }
    }
}
